package edu.ynu.se.xiecheng.achitectureclass.camera.controller;

import edu.ynu.se.xiecheng.achitectureclass.camera.service.CameraService;
import edu.ynu.se.xiecheng.achitectureclass.device.entity.Device;
import edu.ynu.se.xiecheng.achitectureclass.device.service.DeviceService;
import edu.ynu.se.xiecheng.achitectureclass.camera.entity.CameraImage;
import edu.ynu.se.xiecheng.achitectureclass.camera.dao.CameraImageDAO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "摄像头管理", description = "摄像头相关接口")
@RestController
@RequestMapping("/api/cameras")
@CrossOrigin(originPatterns = "*", allowCredentials = "true")
public class CameraController {

    @Autowired
    private CameraService cameraService;
    
    @Autowired
    private DeviceService deviceService;
    
    @Autowired
    private CameraImageDAO cameraImageDAO;
    
    @Operation(summary = "获取萤石云Token")
    @PostMapping("/token")
    public ResponseEntity<?> getToken() {
        try {
            Map<String, Object> result = cameraService.getYsToken();
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            return ResponseEntity.badRequest()
                    .body(Map.of(
                            "success", false,
                            "message", "获取Token失败: " + e.getMessage()
                    ));
        }
    }
    
    @Operation(summary = "抓拍摄像头图片")
    @PostMapping("/{deviceNumber}/capture")
    public ResponseEntity<?> captureImage(
            @PathVariable String deviceNumber,
            @RequestParam String accessToken) {
        try {
            Map<String, Object> result = cameraService.captureImage(deviceNumber, accessToken);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            return ResponseEntity.badRequest()
                    .body(Map.of(
                            "success", false,
                            "message", "抓拍失败: " + e.getMessage()
                    ));
        }
    }
    
    @Operation(summary = "获取摄像头历史图片")
    @GetMapping("/{deviceNumber}/images")
    public ResponseEntity<?> getImages(@PathVariable String deviceNumber) {
        try {
            Device device = deviceService.getDeviceByNumber(deviceNumber);
            if (device == null) {
                return ResponseEntity.badRequest()
                        .body(Map.of(
                                "success", false,
                                "message", "设备不存在"
                        ));
            }
            
            List<CameraImage> images = cameraImageDAO.findByDeviceOrderByCaptureTimeDesc(device);
            return ResponseEntity.ok(images);
        } catch (Exception e) {
            return ResponseEntity.badRequest()
                    .body(Map.of(
                            "success", false,
                            "message", "获取历史图片失败: " + e.getMessage()
                    ));
        }
    }
} 